<?php
  error_reporting(E_ALL ^ E_DEPRECATED);

  mysql_connect('localhost', 'root', '');
  @mysql_select_db('phpbb') or die( "Unable to select database");

  $query = "SELECT * FROM `favl_clubes`";
  $result = mysql_query($query);

  if (!$result) {
      echo "Could not successfully run query ($sql) from DB: " . mysql_error();
      exit;
  }

  if (mysql_num_rows($result) == 0) {
      echo "No rows found, nothing to print so am exiting";
      exit;
  }

  $entries = array();

  while ($row = mysql_fetch_assoc($result)) {
      $entries[] = array(
        'id' => $row["ID"], 
        'name' => $row["nombre"], 
        'long_name' => $row["nombre_largo"], 
        'email' => $row["email"],
        'email2' => $row["email2"],
        'website' => $row["web"],
        'active' => $row["activo"] == 'SI'? true : false,
        'logo_path' => $row["logo"],
        'address_line_1' => $row["domicilio"],
        'location' => $row["provincia"],
        'country_code' => 'AR'
      );
  }

  mysql_free_result($result);

  foreach ($entries as $key => $value) {
    //var_dump($value);
    $query = "insert into portal_club(name,long_name,email,website,active,logo_path,address_line_1,location,country_code) values(".
      "'".$value['name']."',".
      "'".$value['long_name']."',".
      (($value['email'] != '')? "'".$value['email']."'" : "NULL").','.
      (($value['website'] != '')? "'".$value['website']."'" : "NULL").','.
      (($value['active'] != '')? "'".$value['active']."'" : "false").','.
      (($value['logo_path'] != '')? "'".$value['logo_path']."'" : "NULL").','.
      (($value['address_line_1'] != '')? "'".$value['address_line_1']."'" : "NULL").','.
      (($value['location'] != '')? "'".$value['location']."'" : "NULL").','.
      (($value['country_code'] != '')? "'".$value['country_code']."'" : "NULL").')';

    echo "$query\n";
    mysql_query($query) or die('Error, insert query failed');
  }

  //var_dump($entries);

  mysql_close();
?>
